package com.example.mqproduce.services;

import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author taritari
 * @date 2023-11-29 16:11
 * @description
 */
@Service
public class OrderService {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void makeOrderFanout(String userId,String productId,int num){
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功:"+orderId);
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }
    public void makeOrderDirect(String userId,String productId,int num){
        String exchangeName = "direct_order_exchange";
        String routingKey = "";
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功:"+orderId);
        rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);

    }
    public void makeOrderTopic(String userId,String productId,int num){
        String exchangeName = "topic_order_exchange";
        String routingKey = "com.email.duanxin";
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功:"+orderId);
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }
    public void makeDirectTtl(String userId,String productId,int num){
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "ttl";
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功:"+orderId);
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }
    public void makeDirectTtlMessage(String userId,String productId,int num){
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "ttlMessage";
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功:"+orderId);
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId,messagePostProcessor);
    }
}
